package com.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;
import sun.net.www.content.image.png;

public class PanelTambahFotoAnggota extends JPanel {

    private JLabel labelKodeAnggota;
    private JTextField textKodeAnggota;
    private JButton tombolSimpan;
    private JButton tombolCariGambar;
    private JFileChooser chooser;
    private File file = null;
    private BufferedImage image;

    public PanelTambahFotoAnggota() {

        inisialisasi();

    }

    public void inisialisasi() {
        file = new File("no_pic.PNG");
        try {
            image = ImageIO.read(file);
            image.getGraphics();
        } catch (IOException ex) {
            Logger.getLogger(PanelTambahFotoAnggota.class.getName()).log(Level.SEVERE, null, ex);
        }

        setLayout(new BorderLayout());
        JPanel panelTombol = new JPanel();
        panelTombol.setLayout(new FlowLayout());
        labelKodeAnggota = new JLabel("Kode");
//        labelKodeAnggota.setBounds(30, 80, 50, 30);
        panelTombol.add(labelKodeAnggota);

        textKodeAnggota = new JTextField();
        textKodeAnggota.setColumns(10);
//        textKodeAnggota.setBounds(85, 80, 50, 30);
        panelTombol.add(textKodeAnggota);
        textKodeAnggota.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textKodeAnggotaAction();
            }
        });


        tombolCariGambar = new JButton("CariGambar");
        tombolCariGambar.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tombolCariGambarAction();
            }
        });
        panelTombol.add(tombolCariGambar);

        tombolSimpan = new JButton("Simpan");
        tombolSimpan.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tombolSimpanAction();
            }
        });
        panelTombol.add(tombolSimpan);
        add(panelTombol, BorderLayout.SOUTH);
    }

    public void tombolSimpanAction() {
        File fileBaru = new File(textKodeAnggota.getText() + ".PNG");

        {
            try {
                FileOutputStream os = new FileOutputStream(fileBaru);
                FileInputStream is = new FileInputStream(file);

            } catch (FileNotFoundException ex) {
                Logger.getLogger(PanelTambahFotoAnggota.class.getName());
            }

        }
    }

    public void tombolCariGambarAction() {
        chooser = new JFileChooser();
        int status = chooser.showOpenDialog(this);
        if (status == JFileChooser.APPROVE_OPTION) {
            file = chooser.getSelectedFile();
            repaint();
            try {
                image = ImageIO.read(file);
            } catch (IOException ex) {
                Logger.getLogger(PanelTambahFotoAnggota.class.getName()).log(Level.SEVERE, null, ex);
            }
            image.getGraphics();
        }
    }

    public void textKodeAnggotaAction() {
        File fileBaru = new File(textKodeAnggota.getText() + ".PNG");
        repaint();
    }

    @Override
    public void paintComponent(Graphics g) {
        g.drawImage(image,
                0, 0, 400, 300,
                0, 0, image.getWidth(null), image.getHeight(null),
                null);
    }
}
